<?php
class sw_perusahaan_model extends MY_Model {
	var $model = 'sw_perusahaan';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	A.ID_PERUSAHAAN,A.KODE_PERUSAHAAN,A.NAMA_PERUSAHAAN,A.NAMA_PENDEK,A.ADDRESS,A.RT,A.RW,A.VILLAGE,A.SUBDISTRICT,
							A.CITY_CODE,A.PROVINCE_CODE,A.COUNTRY_CODE,A.ZIPCODE,A.PHONE,A.FAX,A.EMAIL,A.WEBSITE,
							TO_CHAR(A.TGL_REGISTRASI,'DD/MM/YYYY') TGL_REGISTRASI, TO_CHAR(A.TGL_AKHIR,'DD/MM/YYYY') TGL_AKHIR, A.ST_AKTIF, 
							B.CITY_NAME, C.PROVINCE_NAME, D.COUNTRY_NAME,
							(SELECT COUNT(1) FROM SIS_ANGGOTA X WHERE X.ID_PERUSAHAAN = A.ID_PERUSAHAAN) JML 
					FROM	SW_PERUSAHAAN A
							LEFT JOIN SW_CITY B ON A.CITY_CODE=B.CITY_CODE
							LEFT JOIN SW_PROVINCE C ON C.PROVINCE_CODE=A.PROVINCE_CODE
							LEFT JOIN SW_COUNTRY D ON D.COUNTRY_CODE=A.COUNTRY_CODE
					WHERE	(UPPER(A.ID_PERUSAHAAN) LIKE '%".strtoupper(post('keyword'))."%' 
							OR UPPER(A.KODE_PERUSAHAAN) LIKE '%".strtoupper(post('keyword'))."%' 
							OR UPPER(A.NAMA_PERUSAHAAN) LIKE '%".strtoupper(post('keyword'))."%' 
							OR UPPER(NVL(A.NAMA_PENDEK,'')) LIKE '%".strtoupper(post('keyword'))."%' 
							OR UPPER(NVL(B.CITY_NAME,'')) LIKE '%".strtoupper(post('keyword'))."%' 
							OR UPPER(NVL(C.PROVINCE_NAME,'')) LIKE '%".strtoupper(post('keyword'))."%' 
							OR UPPER(NVL(D.COUNTRY_CODE,'')) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'A.KODE_PERUSAHAAN' : post('sort'))." ".(post('order')=='' ? 'ASC' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	function Add($arr_data){
		$this->db->insert('SW_PERUSAHAAN', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('SW_PERUSAHAAN', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_id){
		$ls_sql	= "	SELECT	A.ID_PERUSAHAAN,A.KODE_PERUSAHAAN,A.NAMA_PERUSAHAAN,A.NAMA_PENDEK,A.ADDRESS,A.RT,A.RW,A.VILLAGE,A.SUBDISTRICT,
							A.CITY_CODE,A.PROVINCE_CODE,A.COUNTRY_CODE,A.ZIPCODE,A.PHONE,A.FAX,A.EMAIL,A.WEBSITE,
							TO_CHAR(A.TGL_REGISTRASI,'DD/MM/YYYY') TGL_REGISTRASI, TO_CHAR(A.TGL_AKHIR,'DD/MM/YYYY') TGL_AKHIR, A.ST_AKTIF, 
							B.CITY_NAME, C.PROVINCE_NAME, D.COUNTRY_NAME
					FROM	SW_PERUSAHAAN A
							LEFT JOIN SW_CITY B ON A.CITY_CODE=B.CITY_CODE
							LEFT JOIN SW_PROVINCE C ON C.PROVINCE_CODE=A.PROVINCE_CODE
							LEFT JOIN SW_COUNTRY D ON D.COUNTRY_CODE=A.COUNTRY_CODE
					WHERE	A.ID_PERUSAHAAN = '".$as_id."'";
		$result = $this->db->query($ls_sql);
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_id)
	{
		$result = $this->db->get_where('SW_PERUSAHAAN', array('ID_PERUSAHAAN' => $as_id));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
				$this->db->delete('SW_PERUSAHAAN', $arr_rule);
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>